package com.jhh.pay.center.api.transformer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jhh.pay.center.api.data.BankInfo;
import com.jhh.pay.center.api.data.OrderR;
import com.jhh.pay.center.model.Order;
import com.jhh.pay.center.model.PayChannel;
import com.jhh.pay.center.search.DocTransformer;
import com.jhh.pay.center.service.PayChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author tangxd
 * @Description: TODO
 * @date 2017/12/15
 */
@Component
public class OrderMappingToOrderRTransformer implements DocTransformer<Order,OrderR> {
    @Autowired
    private PayChannelService channelService;

    @Override
    public OrderR transform(Order data) {
        if(data == null) {
            return null;
        }
        OrderR r = new OrderR();
        r.setSid(data.getId());
        r.setOrderNo(data.getOrderNo());
        r.setAppId(data.getAppId());
        r.setMoney(data.getMoney());
        r.setOrderTime(data.getOrderTime());
        if(data.getChannelId() > 0) {
            PayChannel channel = channelService.get(data.getChannelId());
            if (channel != null) {
                r.setChannelId(channel.getId());
                r.setChannelName(channel.getName());
            }
        }
        r.setChannelCustomNo(data.getChannelOrderNo());
        BankInfo bank = new BankInfo();
        bank.setBankCard(data.getBankCard());
        bank.setBankMobile(data.getBankMobile());
        bank.setBankNo(data.getBankNo());
        bank.setPersonalName(data.getPersonalName());
        r.setBankInfo(bank);
        r.setChannels((JSONObject)JSON.parse(data.getChannels()));
        r.setExtension((JSONObject)JSON.parse(data.getExtension()));
        r.setPostTime(data.getPostTime());
        r.setOrderState(data.getState().code());
        r.setLastResp(data.getLastResp());
        r.setLastRespTime(data.getLastRespTime());
        r.setNotifyState(data.getNotifyState().code());
        r.setProperties((JSONObject)JSON.parse(data.getProperties()));
        return r;
    }
}
